"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.ServerConfig = void 0;
const Logger_1 = require("../engine/utils/Logger");
const RandomUtil_1 = require("../engine/utils/RandomUtil");
const path = require("path");
const fs = require("fs");
const Config_1 = require("./Config");
const GameConfig_1 = require("./GameConfig");
class ServerConfig {
    constructor() {
        this.serverType = "";
        this.id = 0;
        this.internalIp = "";
        this.http_port = 0;
        this.socket_port = 0;
        this.status = 1;
        this.nowPort = 0;
        this.onlineNum = 0;
        this.useridList = [];
    }
    static readConfigFile() {
        return __awaiter(this, void 0, void 0, function* () {
            let configPath = path.resolve() + "/config/";
            if (Config_1.Config.environment == Config_1.Config.EnvironmentType.Dev) {
                configPath = configPath + "dev.json";
            }
            else if (Config_1.Config.environment == Config_1.Config.EnvironmentType.Test) {
                configPath = configPath + "test.json";
            }
            else if (Config_1.Config.environment == Config_1.Config.EnvironmentType.Online) {
                configPath = configPath + "online.json";
            }
            else {
                Logger_1.Logger.warn("不存在的环境配置文件", configPath);
            }
            // Logger.log("readConfigFile==", configPath)
            let data = yield fs.readFileSync(configPath);
            // Logger.log("readConfigFile=data=", data.toString())
            let configObj = JSON.parse(data.toString());
            return configObj;
        });
    }
    static initOneServerType(configObj, nowPort) {
        let config = new ServerConfig();
        config.ip = configObj["ip"];
        config.id = configObj["id"];
        config.internalIp = configObj["internalIp"];
        config.http_port = configObj["http_port"];
        config.socket_port = configObj["socket_port"];
        config.status = configObj["status"];
        if (nowPort == config.http_port || nowPort == config.socket_port) {
            ServerConfig.nowServerConfig = config;
            ServerConfig.nowServerConfig.nowPort = nowPort;
        }
        return config;
    }
    static initConfig(nowPort) {
        return __awaiter(this, void 0, void 0, function* () {
            let configObj = yield this.readConfigFile();
            let serverList = configObj[Config_1.Config.ServerType_List.WebServer];
            this.WebServer_List = [];
            for (let i = 0; i < serverList.length; i++) {
                let config = this.initOneServerType(serverList[i], nowPort);
                if (config.http_port == 9600) {
                    this.WebServer_List[i] = config;
                    this.WebServer_List[i].serverType = Config_1.Config.ServerType_List.WebServer;
                }
            }
            serverList = configObj[Config_1.Config.ServerType_List.account_server];
            this.account_server_List = [];
            for (let i = 0; i < serverList.length; i++) {
                this.account_server_List[i] = this.initOneServerType(serverList[i], nowPort);
                this.account_server_List[i].serverType = Config_1.Config.ServerType_List.account_server;
            }
            // serverList = configObj[Config.ServerType_List.gm_server]
            // this.gm_server_list = []
            // for (let i = 0; i < serverList.length; i++) {
            //     this.gm_server_list[i] = this.initOneServerType(serverList[i], nowPort)
            //     this.gm_server_list[i].serverType = Config.ServerType_List.gm_server
            // }
            // serverList = configObj[Config.ServerType_List.log_server]
            // this.log_server_list = []
            // for (let i = 0; i < serverList.length; i++) {
            //     this.log_server_list[i] = this.initOneServerType(serverList[i], nowPort);
            //     this.log_server_list[i].serverType = Config.ServerType_List.log_server
            // }
        });
    }
    static randomWebServer() {
        return RandomUtil_1.default.randomItem(this.WebServer_List);
    }
    static randomAccountServer() {
        return RandomUtil_1.default.randomItem(this.account_server_List);
    }
    static randomHallServer() {
        return RandomUtil_1.default.randomItem(this.hall_server_List);
    }
    static randomLogServer() {
        return RandomUtil_1.default.randomItem(this.log_server_list);
    }
    static randomPayServer() {
        return RandomUtil_1.default.randomItem(this.pay_server_List);
    }
    getHttpUrl() {
        return "http://" + this.ip + ":" + this.http_port;
    }
    getInternalUrl() {
        return "http://" + this.internalIp + ":" + this.http_port;
    }
    getInternalWsUrl() {
        let url = "ws://" + this.internalIp + ":" + this.socket_port;
        return url;
    }
    static randomGameServer(gameType) {
        let resultList = [];
        if (gameType == GameConfig_1.GameConfig.Game_Type.Mj) {
        }
        // else if (gameType == GameConfig.Game_Type.ddz) {
        //     resultList = this.ddz_server_list
        // }
        // else if (gameType == GameConfig.Game_Type.jianziding) {
        //     resultList = this.jianziding_server_list;
        // }
        // else if (gameType == GameConfig.Game_Type.cangxian_mj) {
        //     resultList = this.cangxian_mj_server_list;
        // }
        // else if (gameType == GameConfig.Game_Type.paodekuai) {
        //     resultList = this.paodekuai_server_list;
        // }
        // else if (gameType == GameConfig.Game_Type.dazhonger_mj) {
        //     resultList = this.dazhonger_mj_server_list;
        // }
        // resultList.sort((a: ServerConfig, b: ServerConfig) => {
        //     return a.useridList.length - b.useridList.length
        // })
        return resultList[0];
    }
    static getAllSocketServer() {
        let result = [];
        result = result.concat(this.hall_server_List);
        // result = result.concat(this.huanghua_mj_server_list)
        // result = result.concat(this.ddz_server_list)
        // result = result.concat(this.jianziding_server_list)
        // result = result.concat(this.cangxian_mj_server_list)
        // result = result.concat(this.paodekuai_server_list)
        // result = result.concat(this.dazhonger_mj_server_list)
        return result;
    }
    static getAllGameServer() {
        let result = [];
        // result = result.concat(this.mj_server_list)
        // result = result.concat(this.huanghua_mj_server_list)
        // result = result.concat(this.ddz_server_list)
        // result = result.concat(this.jianziding_server_list)
        // result = result.concat(this.cangxian_mj_server_list)
        // result = result.concat(this.paodekuai_server_list)
        // result = result.concat(this.dazhonger_mj_server_list)
        return result;
    }
    static getGameServerById(server_id) {
        let serverList = this.getAllGameServer();
        for (let i = 0; i < serverList.length; i++) {
            let serverConfig = serverList[i];
            if (serverConfig.id == server_id) {
                return serverConfig;
            }
        }
        return null;
    }
    static getServerByIdAndType(server_id, serverType) {
        let nowServer;
        let serverList = [];
        // if (serverType == Config.ServerType_List.huanghua_mj_server) {
        //     serverList = this.huanghua_mj_server_list
        // }
        // else if (serverType == Config.ServerType_List.ddz_server) {
        //     serverList = this.ddz_server_list
        // }
        // else if (serverType == Config.ServerType_List.cangxian_mj_server) {
        //     serverList = this.cangxian_mj_server_list
        // }
        // else if (serverType == Config.ServerType_List.paodekuai_server) {
        //     serverList = this.paodekuai_server_list
        // }
        // else if (serverType == Config.ServerType_List.jianziding_server) {
        //     serverList = this.jianziding_server_list
        // }
        // else if (serverType == Config.ServerType_List.dazhonger_mj_server) {
        //     serverList = this.dazhonger_mj_server_list
        // }
        for (let i = 0; i < serverList.length; i++) {
            let server = serverList[i];
            if (server.id == server_id) {
                nowServer = server;
                break;
            }
        }
        return nowServer;
    }
    static update_online(server_id, serverType, userids) {
        let nowServer = this.getServerByIdAndType(server_id, serverType);
        if (nowServer) {
            nowServer.useridList = userids;
            nowServer.onlineNum = userids.length;
        }
    }
}
exports.ServerConfig = ServerConfig;
ServerConfig.Status_Type = {
    Normal: 1,
    Error: 998,
    Weihu: 999,
};
ServerConfig.upload_server_list = [];
ServerConfig.gm_server_list = [];
ServerConfig.pay_server_List = [];
ServerConfig.WebServer_List = [];
ServerConfig.account_server_List = [];
ServerConfig.hall_server_List = [];
ServerConfig.log_server_list = [];
